package com.bmadmin.service.impl;

import com.bmadmin.domain.DataQueryItemVariablePOJO;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.mapper.DataQueryItemVariablePOJODao;
import com.bmadmin.service.DataQueryItemVariableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 金玮良 on 2017-12-21.
 */
@Service
@Transactional
public class DataQueryItemVariableServiceImpl implements DataQueryItemVariableService {
    @Autowired
    DataQueryItemVariablePOJODao dataQueryItemVariablePOJODao;

    @Override
    public LayUiDataTablePOJO getVariableList(DataQueryItemVariablePOJO dataQueryItemVariablePOJO) {
        LayUiDataTablePOJO result = new LayUiDataTablePOJO();

        List<DataQueryItemVariablePOJO> variablePOJOS = dataQueryItemVariablePOJODao.find(dataQueryItemVariablePOJO);

        result.setCode(0);
        result.setMsg("查询成功");
        result.setData(variablePOJOS);
        result.setCount(variablePOJOS.size());

        return result;
    }

    @Override
    public NormalResult insertQueryItemVariable(DataQueryItemVariablePOJO dataQueryItemVariablePOJO) {
        NormalResult result = new NormalResult();

        DataQueryItemVariablePOJO dataQueryItemVariablePOJOForQueryName = new DataQueryItemVariablePOJO();
        dataQueryItemVariablePOJOForQueryName.setV_name(dataQueryItemVariablePOJO.getV_name());

        List<DataQueryItemVariablePOJO> variablePOJOS = dataQueryItemVariablePOJODao.find(dataQueryItemVariablePOJOForQueryName);
        if(variablePOJOS.size()>0){
            result.setCode("9999");
            result.setMsg("变量名不得重复");
            return result;
        }


        dataQueryItemVariablePOJODao.insertSelective(dataQueryItemVariablePOJO);
        result.setCode("0000");
        result.setMsg("插入成功");

        return result;
    }

    @Override
    public NormalResult updateQueryItemVariable(DataQueryItemVariablePOJO dataQueryItemVariablePOJO) {
        NormalResult result = new NormalResult();

        DataQueryItemVariablePOJO dataQueryItemVariablePOJOForQueryName = new DataQueryItemVariablePOJO();
        dataQueryItemVariablePOJOForQueryName.setV_name(dataQueryItemVariablePOJO.getV_name());

        List<DataQueryItemVariablePOJO> variablePOJOS = dataQueryItemVariablePOJODao.find(dataQueryItemVariablePOJOForQueryName);
        if(variablePOJOS.size()>0){

            if(variablePOJOS.size()==1){
                if(!dataQueryItemVariablePOJO.getV_id().equals(variablePOJOS.get(0).getV_id())){
                    result.setCode("9999");
                    result.setMsg("变量名不得重复");
                    return result;
                }

            }else{
                result.setCode("9999");
                result.setMsg("变量名不得重复");
                return result;
            }
        }

        dataQueryItemVariablePOJODao.update(dataQueryItemVariablePOJO);
        result.setCode("0000");
        result.setMsg("修改成功");

        return result;
    }

    @Override
    public NormalResult delQueryItemVariable(DataQueryItemVariablePOJO dataQueryItemVariablePOJO) {
        NormalResult result = new NormalResult();

        dataQueryItemVariablePOJODao.deleteByV_id(dataQueryItemVariablePOJO.getV_id());
        result.setCode("0000");
        result.setMsg("删除成功");

        return result;
    }
}
